package com.woniuxy.kaer.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.system.dto.ResourceDTO;
import com.woniuxy.kaer.system.mapper.RoleMapper;
import com.woniuxy.kaer.system.mapper.RoleResourceMapper;
import com.woniuxy.kaer.system.model.Resources;
import com.woniuxy.kaer.system.mapper.ResourceMapper;
import com.woniuxy.kaer.system.model.Role;
import com.woniuxy.kaer.system.model.RoleResource;
import com.woniuxy.kaer.system.param.ResourceParam;
import com.woniuxy.kaer.system.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resources> implements ResourceService {

    @Resource
    private ResourceMapper resourceMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleResourceMapper roleResourceMapper;





    @Override
    public List<ResourceDTO> findAllResource() throws Exception {
        ArrayList<ResourceDTO> resourceList = new ArrayList<>();
        // 获取所有根资源，即parent为0的资源
        List<Resources> resources = resourceMapper.selectList(new QueryWrapper<Resources>().eq("parent", 0));
        for (Resources resource : resources) {
            // 将Resource对象转换为ResourceDto
            ResourceDTO resourceDTO = BeanUtil.toBean(resource, ResourceDTO.class);
            // 递归查找子资源并赋值
            findChildren(resourceDTO);
            resourceList.add(resourceDTO);
        }

        return resourceList;
    }

    @Override
    public List<Resources> getRoleResource(Integer roleId) throws Exception {
        // 查询 role_resource 表中所有与该角色关联的资源ID
        List<String> resourceIds = resourceMapper.selectResourceByRoleId(roleId);

        // 如果没有资源，返回空列表
        if (resourceIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 根据资源ID列表查询所有资源
        return resourceMapper.selectBatchIds(resourceIds);
    }

    @Override
    public List<Resources> getAllResources(Integer roleId) throws Exception {
        //判断角色是否存在
        Role role = roleMapper.selectById(roleId);
        if(role==null){
            throw new Exception("角色不存在");
        }
        ArrayList<Resources> list = new ArrayList<>();
        //通过角色id获取角色权限
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        List<RoleResource> roleResources = roleResourceMapper.selectList(wrapper);
        if(roleResources.size()==0){
            return list;
        }
        //获取权限列表
        List<Integer> resourceIds = roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList());
        List<Resources> resources = resourceMapper.selectBatchIds(resourceIds);
        list.addAll(resources);
        return list;
    }


    private void findChildren(ResourceDTO resourceDTO) throws Exception {
        // 获取当前资源的所有子资源，通过resourceDTO.getResourceId()获取当前资源的ID，作为查询条件

        List<Resources> resources = resourceMapper.selectList(new QueryWrapper<Resources>().eq("parent", resourceDTO.getResourceId()));

        // 遍历子资源，转换为DTO并加入children集合
        for (Resources resource : resources) {
            ResourceDTO childResourceDTO = BeanUtil.toBean(resource, ResourceDTO.class);
            System.out.println(childResourceDTO+"123123");
            // 将子资源添加到当前资源的children中
            resourceDTO.getChildren().add(childResourceDTO);
            // 递归查找子资源的子资源
            findChildren(childResourceDTO);
        }
    }





}




